package com.sh.data.engine.repository.dao.integration.online.entity;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.sh.data.engine.common.enumDefinition.OfflineSyncTaskStatus;
import com.sh.data.engine.repository.dao.base.EngineBaseModel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@EqualsAndHashCode(callSuper = true)
@TableName(value = "tb_flink_data_sync_task", autoResultMap = true)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class FlinkDataSyncTaskEntity extends EngineBaseModel<FlinkDataSyncTaskEntity> {

    private Integer xxlJobInfoId;

    private Integer xxlJobLogId;

    /**
     * flink 任务
     */
    private Long projectId;

    /**
     * 任务名称
     */
    private String taskName;

    /**
     * 日志id（自己写入文件）
     */
    private Long recordId;

    private Long dirId;

    /**
     * 任务描述
     */
    private String remark;

    /**
     * 来源数据源id
     */
    private Long sourceDsId;

    /**
     * 来源数据源类型
     */
    private String sourceDsType;

    /**
     * 目标数据源id
     */
    private Long targetDsId;

    /**
     * 来源数据源类型
     */
    private String targetDsType;

    private String sourceTableOrTopic;

    private String targetTableOrTopic;

    private String sourceSchema;

    private String targetSchema;

    private Integer start;
    // 发布、待发布
    private Integer taskStatus;
    // 运行中、停止
    private Integer workingStatus;
    private Date startTime;
    private Date endTime;

    private String flinkJobId;

    private String userId;

    // 0-默认 1-jsonpath脚本 2-velocity脚本
    private Integer useJsFlag;

    private String jsContent;

    /** 以下为Mq的record记录信息 */
    /**
     * taskId 16位MD5加密
     */
    private String taskIdMd5;

    /**
     * 延时毫秒
     */
    private Integer delay;

    /**
     * 最近一次数据同步时间
     */
    private Date lastSyncTime;

    /**
     * 任务启动点时间
     */
    private Date startPointTime;

    /**
     * 同步失败条数
     */
    private Integer failNum;

    private Integer jmxPort;

    private String jmxServer;

    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<String> tableData;

    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<FiledExpess> fieldExpressRequests;

    public List<FiledExpess> getFieldExpressRequests() {
        if (Objects.nonNull(fieldExpressRequests) && CollectionUtils.isNotEmpty(fieldExpressRequests)) {
            Object filedExpess = fieldExpressRequests.get(0);
            if (filedExpess instanceof FiledExpess) {
                return fieldExpressRequests;
            }
            List<FiledExpess> fieldExpressRequestList =
                JSONArray.parseArray(fieldExpressRequests.toString(), FiledExpess.class);
            return fieldExpressRequestList;
        }
        return null;
    }

    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<ParitionFieldMap> paritionFieldMaps;

    public List<ParitionFieldMap> getParitionFieldMaps() {
        if (Objects.nonNull(paritionFieldMaps) && CollectionUtils.isNotEmpty(paritionFieldMaps)) {
            Object paritionFieldMap = paritionFieldMaps.get(0);
            if (paritionFieldMap instanceof ParitionFieldMap) {
                return paritionFieldMaps;
            }
            List<ParitionFieldMap> paritionFieldMapList =
                JSONArray.parseArray(paritionFieldMaps.toString(), ParitionFieldMap.class);
            return paritionFieldMapList;
        }
        return null;
    }

    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<FieldMapping> fieldMappings;

    public List<FieldMapping> getFieldMappings() {

        if (Objects.nonNull(fieldMappings) && CollectionUtils.isNotEmpty(fieldMappings)) {
            Object fieldMapping = fieldMappings.get(0);
            if (fieldMapping instanceof FieldMapping) {
                return fieldMappings;
            }
            List<FieldMapping> fieldMappingList =
                JSONArray.parseArray(fieldMappings.toString(), FieldMapping.class);
            return fieldMappingList;
        }
        return null;
    }

    private Integer isAlarmed;

    private Integer taskRunStatus = OfflineSyncTaskStatus.unrunning.getTaskStatus();

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class FieldMapping {

        private String sourceField;

        private String sourceFieldType;

        private boolean isSourceFieldPk;

        private boolean isSourcePartition;

        private String targetField;

        private String targetFieldType;

        private boolean isTargetFieldPk;

        private boolean isTargetPartition;

        private boolean isTargetTag;

        private String sourceFieldExpress;
    }

    @Data
    public static class FiledExpess {

        private String fieldName;

        private String fieldExpress;
    }

    @Data
    public static class ParitionFieldMap {

        /**
         * 字段名称
         */
        private String field;

        /**
         * 字段值
         */
        private String value;
    }
}
